package com.wang.jmonkey.modules.log.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.wang.jmonkey.common.constant.RabbitConstant;
import com.wang.jmonkey.modules.log.model.entity.LogHandleEntity;
import com.wang.jmonkey.modules.log.service.LogHandleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description 接收操作日志信息
 * @Author HeJiawang
 * @Date 2021/5/21 15:53
 */
@Slf4j
@Component
@RabbitListener(queues = {RabbitConstant.QUEUE_LOG_HANDLE})
public class LogHandleListener {

    /**
     * service
     */
    @Autowired
    private LogHandleService logHandleService;

    /**
     * 线程池
     */
    ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    @RabbitHandler
    public void process(String content) {
        threadPool.execute(() -> {

            try {
                if (StrUtil.isEmpty(content))  return;

                LogHandleEntity entity = new LogHandleEntity().setContent(content);
                logHandleService.save(entity);
            } catch (Exception e) {

                log.error(this.getClass().getName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
            }
        });
    }
}
